﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.SiteAdmin.BusinessLogic;
using Ivy.SiteAdmin.IBusinessLogic;
using Ivy.SiteAdmin.Entity;

using Ivy.Common.Framework.GeneralUtility;

namespace DasIdeen.UserControls
{
    public partial class ContentSummaryControl : System.Web.UI.UserControl
    {
        #region  Variable & constant
        //All constants and local variable should be declared here
        //int lContentKey = 1;
        ContentStatistics lContentStatistics = null;
        const string constContentTypeCode = "DNL";

        #endregion  Variable & constant

        #region Constructor & Destructor
        //Generally not needed in UI layer
        #endregion Constructor & Destructor

        #region Properties
        //UI layer may have public custom properties. these are very handy for User Controls
        //public int ContentKey
        //{
        //    get
        //    {
        //        return lContentKey;
        //    }
        //    set
        //    {
        //        lContentKey = value;                      
        //    }

        //}
        public ContentStatistics ContentStatisticsEntity
        {
            get
            {
                return lContentStatistics;
            }
            set
            {
                lContentStatistics = value;
            }

        }
        #endregion Properties

        #region Public Methods
        //UI layer may have public custom Methods. these are very handy for User Controls
        public override void DataBind()
        {
            try
            {
                if (this.ContentStatisticsEntity != null)
                {
                    TranslateEntityToUIControls(this.ContentStatisticsEntity);
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }
        public void DataBind(ContentStatistics pContentStatistics)
        {
            try
            {
                if (pContentStatistics != null)
                {
                    this.ContentStatisticsEntity = pContentStatistics;
                    DataBind();
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            try //All events in pages or user control should have try{} catch {} block.
            {
                if (!Page.IsPostBack)
                {
                    //First check if the current logged in user is having proper role/authorization to access this page or not.
                    if (IsUserAutherization())
                    {
                        //TODO - Your code to display the initial layout/data etc.
                    }
                    else
                    {
                        //TODO - Your code to redirect page to "NOT AUTHORIZED" page or any other page as required by the business logic.
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        #endregion Events

        #region Private Methods

        /// <summary>
        /// This method is to check if the current logged in user is having proper role/authorization to access this page or not.
        /// </summary>
        private bool IsUserAutherization()
        {
            bool _IsUserAutherization = false;
            //TODO - Your code to check authorization goes here.

            //for the time being let us allow the user to access this page.
            //but remove the below line once you have a proper logic.
            _IsUserAutherization = true;

            return _IsUserAutherization;
        }

        /// <summary>
        /// This method is to clear all UI controls which are data binded.
        /// </summary>
        private void ClearScreen()
        {
        }

        /// <summary>
        /// This method is to bind all the UI control like dropdown list or list box etc with the required initial data, 
        /// </summary>
        private void FillLoadLookUpData()
        {
        }

        /// <summary>
        /// This method is to load the UI with initial set of data based of the business logic.
        /// </summary>
        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
        }

        /// <summary>
        /// This method is to copy the entity properties that need to be displayed in the UI controls to the UI control.
        /// Example - [pUser] is an instance of an entity and is filled with specific USER data.
        /// and this user instance is to be displayed is an UI control text boxes.
        /// A sample code will look like the following:
        ///  this.txtFirstName.Text = pUser.UserFirstName; 
        ///  this.txtLastName.Text  = pUser.UserLastName; 
        ///  this.txtLoginId.Text   = pUser.UserLoginID;
        /// </summary>
        private void TranslateEntityToUIControls(ContentStatistics pContentStatistics)
        {
            this.lnkContentName.Text = pContentStatistics.ContentName.ToString();
            //if (pContentStatistics.ContentTypeCode != "VDO")
            //{
                this.lnkContentName.NavigateUrl = ("~/Pages/ContentDetails.aspx?ContentKey=" + pContentStatistics.ContentKey);
            //}
            //else
            //{
            //    this.lnkContentName.NavigateUrl = ("~/Pages/VideoContentDetails.aspx?ContentKey=" + pContentStatistics.ContentKey);
            //}
            this.lblLike.Text = pContentStatistics.ContentTotalLikes.ToString();
            this.lblViews.Text = pContentStatistics.ContentTotalViews.ToString();
            this.lblRating.Text = pContentStatistics.AvrageRating.ToString();
            this.lblTotalVotes.Text = pContentStatistics.TotalVotes.ToString();
            if (pContentStatistics.ContentTypeCode != constContentTypeCode)
            {
                this.DivContentDownloadImg.Visible = false;
            }
            else
            {
                this.DivContentDownloadImg.Visible = true;
            }
            this.lblTotalDownloads.Text = pContentStatistics.ContentTotalDownloads.ToString();
            this.lblDate.Text = pContentStatistics.Sys_DateOfCreation.ToShortDateString();
            this.lblAuthorName.Text = pContentStatistics.UserFirstName + " " + pContentStatistics.UserLastName;
            //this.lblContentDescription.Text = StringUtility.ConvertHtmlToString(pContentStatistics.ContentHtmlData.ToString()).Substring(0, 185) + "   ...";
            if (pContentStatistics.ContentMetaTagDescription.Length > 185)
            {
                this.lblContentDescription.Text = pContentStatistics.ContentMetaTagDescription.Substring(0, 185) + "   ...";
            }
            else
            {
                this.lblContentDescription.Text = pContentStatistics.ContentMetaTagDescription + "   ...";
            }
            
        }


        /// <summary>
        /// This method is to copy the user entered data from the UI controls to the entity instance that will be used to sent the entity to any other layer.
        /// Example - [pUser] is an instance of an entity and is to be filled with specific USER data that has been captured in the screen.
        /// A sample code will look like the following:
        ///  pUser.UserFirstName = this.txtFirstName.Text; 
        ///  pUser.UserLastName  = this.txtLastName.Text; 
        ///  pUser.UserLoginID   = this.txtLoginId.Text; 
        /// </summary>
        private void TranslateUIControlsToEntity()
        {
        }

        #endregion Private Methods


    }
}